﻿namespace FFF.Test.Application.DomainServices.FW
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Abp.Domain.Services;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore.Uow;
    using Abp.EntityFrameworkCore;
    using AutoMapper;
    using Abp.UI;
    using Ydm.Helper;
    using Ydm.Extensions;
    using Microsoft.EntityFrameworkCore;
    using AutoMapper.QueryableExtensions;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Application.Repositories.FW;
    using FFF.Test.Entity.FW;
    using FFF.Test.Entity.FW.Dto;
    using FFF.Test.Entity.FW.View;

    /// <summary>
    /// 动作表领域层服务接口
    /// </summary>
    public class FWActionDomainService : YdmAbpDomainServiceBase, IFWActionDomainService
    {
        /// <summary>
        /// 动作表仓储
        /// </summary>
        private readonly IFWActionRepository _fWActionRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="FWActionDomainService"/> class.
        /// 动作表领域层操作
        /// </summary>
        /// <param name="fWActionRepository">动作表仓储</param>
        public FWActionDomainService(
            IFWActionRepository fWActionRepository
        )
        {
            this._fWActionRepository = fWActionRepository;
        }

        /// <summary>
        /// 1.006  初始化动作点(于大明,App)
        /// </summary>
        /// <param name="inDto">初始化动作点输入dto</param>
        /// <returns>初始化动作点输出dto</returns>
        public async Task<InitActionOutDto> InitActionAsync(InitActionInDto inDto)
        {
            Dictionary<string, string> actionCodes = new Dictionary<string, string>
            {
                { "00", "查看" },
                { "01", "新增" },
                { "02", "编辑" },
                { "03", "删除" },
                { "91", "导入" },
                { "92", "导出" },
            };

            foreach (var codeItem in actionCodes)
            {
                var viewAction = this._fWActionRepository.GetView();
                if (!viewAction.Any(x => x.FunctionGUID == inDto.FunctionGUID && x.ActionCode == codeItem.Key))
                {
                    FWActionCreateInputDto inputDto = new FWActionCreateInputDto()
                    {
                        FunctionGUID = inDto.FunctionGUID,
                        ActionName = codeItem.Value,
                        ActionCode = codeItem.Key,
                        ActionRemark = "系统自动生成"
                    };

                    await this._fWActionRepository.CreateByDtoAsync(inputDto);
                }
            }

            var result = new InitActionOutDto();
            return result;
        }
    }
}
